package de.koller.worlders.engine.entity;

import de.koller.oceanFramework.gl.util.ChildDisplayer;
import de.koller.oceanFramework.util.MOut;
import de.koller.worlders.WSea;
import org.lwjgl.input.Keyboard;

public class WorldersIO extends WSea {

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		if( wroot.getInput().isKeyPressed( Keyboard.KEY_F5 ) )
			save();
		if( wroot.getInput().isKeyPressed( Keyboard.KEY_F8 ) )
			ChildDisplayer.show( wroot );
	}
	
	public void save() {
		savePlayer();
		saveMaps();
		wroot.getProgress().save();
		wroot.inventory.save();
		System.out.println( "[Gespeichert]" );
	}
	
	private void saveMaps() {
		wroot.getTileMap().tileHolder.save();
	}
	
	private void savePlayer() {
		MOut o = new MOut( wroot.getUser().getUserpath() + "player.2mp" );
		o.writeFloat( wroot.getEntityContainer().getReseter().getXf() );
		o.writeFloat( wroot.getEntityContainer().getReseter().getYf() );
		
		byte[] mn = wroot.getUser().getMapName().getBytes();
		o.writeInt( mn.length );
		o.write( mn );
		o.close();
		System.out.println( "Player saved" );
	}
	
}
